<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/19
 * Time: 下午8:12
 */

class ModNoticeSet extends Model {

    protected $table = 'notice_set';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function select($params){
        $where  = ' WHERE platform=:platform AND game=:game ';
        $args   = array(
            ':platform' => 'test',
            ':game'     => $params['g']
        );
        
        if(!empty($params['server'])){
        	$where .= ' AND FIND_IN_SET("'.$params['server'].'",server_id) ';
        }

        if(!empty($params['title'])){
            $where .= ' AND title LIKE :title ';
            $args[':title'] = '%'.$params['title'].'%';
        }
        
        if(!empty($params['synchro_time'])){
        	$where .= ' AND synchro_time = :synchro_time ';
        	$args[':synchro_time'] = $params['synchro_time'];
        }
        
        if(!empty($params['synchro_type'])){
        	$where .= ' AND synchro_type = :synchro_type ';
        	$args[':synchro_type'] = $params['synchro_type'];
        }
        
        if(!empty($params['create_user'])){
        	$where .= ' AND create_user = :create_user ';
        	$args[':create_user'] = $params['create_user'];
        }


        $sql = "SELECT * FROM {$this->table} {$where} ORDER BY create_time DESC";

        $result = $this->getDbConnection()->getAll($sql, $args);
        return $result;
    }
    
    public function lastOne($channel_id,$type){
    	$modChannel = new ModChannel();
    	$obj = $modChannel->findByAttributes(array(
    			'tab' => $channel_id
    	));
    	if(empty($obj)){
    		$result = array('status'=>false,'msg'=>'渠道不存在','data'=>'' );
    		return $result;
    	}
    	$ch_id = $obj->id;

    	$sql = "SELECT * FROM {$this->table} where FIND_IN_SET({$ch_id},server_id) and FIND_IN_SET({$type},type) and synchro_time <> 0 ORDER BY create_time DESC limit 1";
    	
    	$data = $this->getDbConnection()->getAll($sql, $args);

    	if($data){
    		$result = array('status'=>true,'msg'=>'查询成功','data'=>$data );
    		return $result;
    	}else{
    		$result = array('status'=>false,'msg'=>'该渠道暂无公告','data'=>'' );
    		return $result;
    	}
    	
    }
} 